var http = require('http')
  , Connect = require('Connect')
  , crypto = require('crypto')
  , algorithm = 'aes-256-ctr'  // 加密方式
  , password = 'miaomiaolove520' //密钥
  , fs = require('fs')
  , db = require('mongodb') // mongodb
  , MongoClient = db.MongoClient
  , assert = require('assert')
  , dbUrl = 'mongodb://localhost:27017/db'
  , io = require('socket.io').listen(8888)
  , Stringfg = '*&*%*$*';
  

  // 加密字符串
  function encrypt(text){
      var cipher = crypto.createCipher(algorithm,password);
      var crypted = cipher.update(text,'utf8','hex');
      crypted += cipher.final('hex');
      return crypted;
  };

  // 解密字符串
  function decrypt(text){
      var decipher = crypto.createDecipher(algorithm,password);
      var dec = decipher.update(text,'hex','utf8');
      dec += decipher.final('utf8');
      return dec;
  };
  
  // debug 这是测试调错模块
  // 受限于node单一的调试方式， 通过把debug信息发送到浏览器来进行调试
  // 浏览器接受到信息之后会绑定到全局的debug函数，具体请看客户端debug.js代码
  function DeBug(data, socket){
      socket.emit('debug', {magess: data});
  };

  // 登录之后用户所拥有的dom
  function LoginDom(b, socket, UserData, name){
      var c = '/rom/admin/html/' + b + '.html';
      fs.readFile( __dirname + c, function(err, data){
          if(err){
              DeBug(err, socket);
          };
          // 发送dom
          socket.emit('user_dom', {magess: data.toString()});
          // 发送用户data
          var  a = new Object();
          a = UserData;
          a.key = encrypt(a.key);
          a.sever_time = Date();
          socket.emit('user_data', {magess: a});
      });
  };
  

  // 客户端连接
  io.sockets.on('connection', function(socket){
      // 连接mongoDB
      MongoClient.connect(dbUrl, function(err, db){
          assert.equal(null, err);
          
          // 接受浏览器本地密码
          socket.on('passport_pass', function(data){
              var user = data.magess;
              var [name, pass] = [user[0], user[1]];
              // 解密字符串
              var pass_txt = decrypt(pass);
              var dbKey = db.collection('key');
              // 查询数据库
              dbKey.find({"_id" : name}).toArray(function(err, docs){
                  assert.equal(err, null);
                  // 发送错误信息
                  if(err){
                      DeBug(err, socket);
                  };
                  // 判断有无数据
                  if(docs.length == 1){
                      if(docs[0].key != undefined){
                          // 判断key字段是否等于pass
                          if(docs[0].key == pass_txt){
                          // 发送正确命令
                              socket.emit('passport_true', {magess:true});
                              var b = docs[0].form;
                              // 登录之后用户所拥有的dom
                              LoginDom(b, socket, docs[0], name);
                          }else{
                              // 发送错误命令
                              socket.emit('passport_true', {magess:false});
                          };
                      };
                  }else
                  if(docs.length == 0){
                      // 发送错误命令
                      socket.emit('passport_true', {magess:false});
                  };
              });
          });
          
          // 接收用户手动输入的帐号密码
          socket.on('passport', function(data){
              var user = data.magess
              var dbKey = db.collection('key');
              // 查询数据库
              dbKey.find({"_id" : user[0]}).toArray(function(err, docs){
                  assert.equal(err, null);
                  if(err){
                      DeBug(err, socket);
                  };
                  if(docs.length == 1){
                      if(docs[0].key == user[1]){
                          // 用户正确
                          socket.emit('passport_true', {magess:true});
                          var b = docs[0].form;
                          LoginDom(b, socket, docs[0], user[0]);
                          // 发送加密过的用户名到浏览器
                          socket.emit('pass_name', {magess: encrypt(user[0])});
                      }else{
                          // 用户错误
                          socket.emit('passport_true', {magess:false});
                      };
                  }else{
                      // 用户错误
                      socket.emit('passport_true', {magess:false});
                  };
              });
          });
          
          // 发送验证码
          socket.on('register-phone', function(data){
              var a = data.magess;
             socket.emit('register-phone-on', {magess: a[1]});
          });
          
          // 接收用户注册请求
          socket.on('register', function(data){
              var passport = data.magess
                , [name, pass] = [passport[0], passport[1]];
              // 连接数据库key表
              var dbKey = db.collection('key');
              // 查询key表
              dbKey.find({"_id" : name}).toArray(function(err, docs){
                  assert.equal(err, null);
                  if(err){
                      DeBug(err, socket);
                  };
                  // 判断有无找到记录
                  if(docs.length == 1){
                      socket.emit('register_true', {magess: false});
                  }else
                  if(docs.length == 0){
                      // 向数据库插入记录
                      if(name != undefined, pass != undefined){
                          dbKey.insertMany([{"_id" : name, "key" : pass, "form" : "root", "down_user" : false, "up_user" : false, "company" : false, "email" : false, "phone" : name, 'course' : false, "time": "7", "up_time": Date(), "name": passport[2], "img":false, "user":false, bell: false}], function(err, result){
                              assert.equal(err, null);
                              if(err){
                                  DeBug(err, socket);
                              };
                              if(result.result.n == 1){
                                  socket.emit('register_true', {magess: true});
                                  fs.mkdir( __dirname + '/rom/user/' + name + '/', function(err) {
                                      if (err) {
                                          return console.error(err);
                                      };
                                  });
                              }else
                              if(result.result.n == 0){
                                  socket.emit('register_true', {magess: false});
                              };   
                          });
                      };
                  };
              });  
          });

          // 接收用户DOM请求
          socket.on('dom', function(data){
              var a = data.magess;
              var b = '/rom/admin/html/' + a + '.html';
              fs.readFile( __dirname + b, function(err, data){
                  if(err){
                      DeBug(err, socket);
                  };
                  // 发送dom
                  socket.emit('dom_on', {magess: data.toString()});
              });
          });
          
          // 判断客户端语言并发送字典
          socket.on('language', function(data){
              var a = data.magess;
              var b = '/rom/admin/txt/' + a + '.txt';
              fs.readFile( __dirname + b, function(err, data){
                  if(err){
                      DeBug(err, socket);
                  };
                  socket.emit('language_dom', {magess: data.toString()});
              });
          });
          
          // 用户模板请求
          socket.on('template', function(data){
              var a = data.magess;
              var b = '/rom/admin/html/' + a + '.html';
              fs.readFile( __dirname + b, function(err, data){
                  if(err){
                      DeBug(err, socket);
                  };
                  socket.emit('template_dom', {magess: data.toString()})
              });
          });
          
          // 用户文件上传
          socket.on('File_name', function(data){
              var a = data.magess;
              var b = a.split(Stringfg);
              var c = decrypt(b[1]);
              socket.on('File', function(data){
                  // 判断文件类型
                  if(b[0].split('\\'||'/')[1] == 'jpg'||'jpeg'||'png'){
                      file_data(c, 'img', b[0]);
                  }else{
                      
                  };
                  
                  function file_data(name, type, age){
                      var b = '/rom/user/' + name + '/' + type + '/' + age;
                      fs.writeFile(__dirname + b, data.magess, function (err) {
                          // 错误处理
                          if(err){
                              DeBug(err, socket);
                              // 判断错误信息头
                              if (err.code == 'ENOENT') {
                                  // 判断错误类型
                                  if(err.syscall == 'open'){
                                      // 新建目录
                                      fs.mkdir( __dirname + '/rom/user/' + name + '/' + type + '/', function(err) {
                                          if (err) {
                                              DeBug(err, socket);
                                          }else{
                                              // 如果无错误重新写入文件
                                              fs.writeFile(__dirname + b, data.magess, function (err) {
                                                  if(err){
                                                      DeBug(err, socket);
                                                  }else{
                                                      socket.emit('File_up', {magess:true});
                                                  };
                                              });
                                          };
                                      });
                                  };
                              };
                          }else{
                              socket.emit('File_up', {magess:true});
                          };
                      });
                  };
              });
          });
          
          
          // 新建课程
          socket.on('NewCourse', function(data){
              var obj = data.magess;
              var dbcourse = db.collection('course');
              dbcourse.find({ "_id": obj[0] }).toArray(function (err, docs) {
                  if(docs.length == 0){
                      dbcourse.insertMany([{"_id":obj[0], "name":obj[1], "y/n": true, "Label": obj[2], "blurb": obj[5], "img": obj[6], "time": obj[3], "obj":  obj[4],  "user_name": obj[7], "admin" : obj[10], "Shared": false, "allot": false}],function(err, result){
                          if(result.result.n == 1){
                              if(obj[0] != undefined){
                                  var dbkey = db.collection('key');
                                  dbkey.find({"_id" : obj[8]}).toArray(function(err, docs){
                                      var a = new Object();
                                      a = docs;
                                      (function (){
                                          if(obj[9] != false){
                                              var coursedata = a[0].course;
                                              coursedata[coursedata.length] = obj[0];
                                              a[0].course = coursedata;
                                              dbkey.updateOne({ "_id" : obj[8]}, { $set: a[0] }, function(err, result) {
                                                  assert.equal(err, null);
                                                  if(result.result.n == 1){
                                                      socket.emit('NewCourseOn', {magess :true});
                                                  }else
                                                  if(result.result.n == 0){
                                                      socket.emit('NewCourseOn', {magess :false})
                                                  };
                                             });
                                          }else{
                                              var coursedata = new Array();
                                              coursedata[coursedata.length] = obj[0];
                                              a[0].course = coursedata;
                                              dbkey.updateOne({ "_id" : obj[8]}, { $set: a[0] }, function(err, result) {
                                                  assert.equal(err, null);
                                                  if(result.result.n == 1){
                                                      socket.emit('NewCourseOn', {magess :true});
                                                  }else
                                                  if(result.result.n == 0){
                                                      socket.emit('NewCourseOn', {magess :false})
                                                  };
                                             }); 
                                          };
                                      })();
                                  });
                              };
                           }else
                           if(result.result.n == 0){
                               socket.emit('NewCourseOn', {magess :false});
                           };
                       });
                      
                  }else
                  if(docs.length == 1){
                      socket.emit('NewCourseOn', {magess :'课程已存在'});
                  }
              });
          });
          
          // 新建课程讲师数
          socket.on('addcourse-username', function(data){
              var dbuser = db.collection('key');
              var name = data.magess;
              dbuser.find({"_id" : name}).toArray(function(err, docs){
                  assert.equal(err, null);
                  if(err){
                      DeBug(err, socket);
                  };
                  if(docs.length != 0){
                      socket.emit('addcourse-username-on', {magess:docs[0]});
                  };
              });
          });
          
          // 用户课程请求
          socket.on('course', function(data){
              // 连接并查询课程库
              var dbKey = db.collection('course');
              var a = data.magess;
              dbKey.find({"_id" : data.magess }).toArray(function(err, docs){
                  assert.equal(err, null);
                  if(err){
                      DeBug(err, socket);
                  };
                  if(docs.length == 1){
                      // 发送课程信息
                      socket.emit('on_course', {magess: docs[0]});
                  };
              });
          });

          // 删除课程
          socket.on('sc_kc', function(data){
              var a = data.magess;
              var dbcourse = db.collection('course');
              dbcourse.find({"_id" : a[0] }).toArray(function(err, docs){
                  assert.equal(err, null);
                  if(err){
                      DeBug(err, socket);
                  };
                  if(docs.length == 1){
                      var b = new Object;
                      b = docs[0];
                      (function c(){
                          b['y/n'] = false;
                          dbcourse.updateOne({ "_id" : a[0]}, { $set: b }, function(err, result) {
                              assert.equal(err, null);
                              if(result.result.n == 1){
                                  socket.emit('sc_kc_on', {magess : true});
                              }else
                              if(result.result.n == 0){
                                  socket.emit('sc_kc_on', {magess : false});
                              };
                          });
                      })();
                  };
              });
          });

          // 更新用户信息
          socket.on('out_user', function(data){
              var a = data.magess;
              var dbkey = db.collection('key');
              dbkey.find({"_id" : a[0] }).toArray(function(err, docs){
                  var b = new Object();
                  b = docs[0];
                  (function c(){
                      b['_id'] = a[0];
                      b.emali = a[3];
                      b.name = a[1];
                      b.company = a[2];
                      b.phone = a[4];
                      dbkey.deleteOne({ "_id" : a[0] }, function(err, result) {
                          assert.equal(err, null);
                          if(result.result.n == 1){
                              dbkey.insertMany([b], function(err, result){
                                  assert.equal(err, null);
                                  if(err){
                                      DeBug(err, socket);
                                  };
                                  if(result.result.n == 1){
                                      socket.emit('out_user', {magess: true});
                                  }else
                                  if(result.result.n == 0){
                                      socket.emit('out_user', {magess: false});
                                  };   
                              });
                          };
                      });
                  })();
              });
          });

          // 更新用户密码
          socket.on('updatakey', function(data){
              var a = data.magess;
              var dbkey = db.collection('key');
              dbkey.find({"_id" : a[0] }).toArray(function(err, docs){
                  var b = new Object();
                  b = docs[0];
                  (function c(){
                      b.key = a[1];
                      dbkey.updateOne({ "_id" : a[0]}, { $set: b }, function(err, result){
                          assert.equal(err, null);
                          if(err){
                              DeBug(err, socket);
                          };
                          if(result.result.n == 1){
                              socket.emit('updatakey_on', {magess: true});
                          }else
                          if(result.result.n == 0){
                              socket.emit('updatakey_on', {magess: false});
                          };   
                      });
                  })();
              });
          });

          // 接受其他用户信息请求
          socket.on('user-data', function(data){
              var a = data.magess;
              var dbkey = db.collection('key');
              dbkey.find({"_id" : a }).toArray(function(err, docs){
                  if(err){
                      DeBug(err, socket);
                  };
                  if(docs.length == 1){
                      var b = docs[0];
                      socket.emit('userdata-on', {magess: b});
                  };
              });
          });
		  
		  
		  // 修改讲师信息
		  socket.on('LoadUserDate', function(data){
			  var a = data.magess;
			  var dbkey = db.collection('key');
			  dbkey.find({"_id" : a[0] }).toArray(function(err, docs){
				  if(err){
                      DeBug(err, socket);
                  };
				  if(docs.length == 0){
					  var b = new Object();
					  b = docs[0];
					  b.key = a[1];
					  b.email = a[2];
					  b.section = a[3];
					  b.name = a[4];
					  b.phone = a[5];
					  b._id = a[0];
					  dbkey.deleteOne({ "_id" : a[0] }, function(err, result) {
						  dbkey.insertMany([b], function(err, result){
						  	if(err){
							  	DeBug(err, socket);
						  	};
							if(result.result.n == 1){
							  	socket.emit('LoadUserDate-on', {magess:true});
						  	}else
						  	if(result.result.n == 0){
							  	socket.emit('LoadUserDate-on', {magess:false});
						  	};
					  	});
					  });
				  }else{
					  socket.emit('LoadUserDate-on', {magess:false});
				  };
			  });
		  });
		  

          // 接受新建讲师请求
          socket.on('upuser', function(data){
              // 接受讲师信息
              var a = data.magess;
              // 链接数据库key表
              var dbkey = db.collection('key');
              dbkey.find({ "_id": a[0] }).toArray(function (err, docs) { 
				  if(err){
					  DeBug(err, socket);
				  };
                  if(docs.length == 0) {
                      // 新建用户
             		  dbkey.insertMany([{"_id" : a[0], "key" : a[1], "form" : "user", "down_user" : false, "up_user" : a[5], "company" : false, "email" : a[2], "phone" : a[4], 'course' : false, "time": "7", "up_time": Date(), "name": a[3], "img":false, "section": a[6], bell:false}], function(err, result){
						  assert.equal(err, null);
						  if(err){
							  DeBug(err, socket);
						  };
						  // 新建成功
						  if(result.result.n == 1){
							  // 查询key表用户
							  dbkey.find({"_id" : a[5] }).toArray(function(err, docs){
								  if(err){
									  DeBug(err, socket);
								  };
								  // 查询成功
								  if(docs.length == 1){
									  var upuserdata = function (c) {
										  // 更新用户表
										  dbkey.updateOne({ "_id" : a[5]}, { $set: c }, function(err, result){
											  assert.equal(err, null);
											  if(err){
												  DeBug(err, socket);
											  };
											  // 更新成功
											  if(result.result.n == 1){
												  socket.emit('upuser-on', {magess: true});
											  }else
											  if(result.result.n == 0){
												  socket.emit('upuser-on', {magess: false});
											  };   
										  });
									  };
									  
									  // 初始化对象
									  var b = docs[0];
									  if(b.down_user == false){
										  var userdata = new Array();
										  userdata[userdata.length] = a[0];
										  b.down_user = userdata;
										  upuserdata(b);
									  }else{
										  b.down_user[b.down_user.length] = a[0];
										  upuserdata(b);
									  };
								  };
							  });
						  };  
					  });
                  };
              });
          });
          
          // 通知与公告
          socket.on('LogBell', function(data){
              var a = data.magess;
			  var dbkey = db.collection('key');
			  dbkey.find({"_id" : a }).toArray(function(err, docs){
				  if(err){
                       DeBug(err, socket);
                   };
				  if(docs.length == 1){
					  socket.emit('LogBell-callback', {magess: docs[0].bell });
				  };
			  });
          });
		  
		  // 课程开发日志
		  socket.on('CourseEditLog', function(data){
			  var a = data.magess;
			  var dbcompany = db.collection('company');
			  dbcompany.find({"_id" : a }).toArray(function(err, docs){
				  if(err){
                       DeBug(err, socket);
                   };
				  if(docs.length == 1){
					  var b = docs[0];
					  socket.emit('CourseEditLog-callback', {magess: b.CourseEditLog });
				  };
			  });
		  });
		  
		  
		  // 讲师使用日志
		  socket.on('UserLog', function(data){
			  var a = data.magess;
			  var dbcompany = db.collection('company');
			  dbcompany.find({"_id" : a }).toArray(function(err, docs){
				  if(err){
                       DeBug(err, socket);
                   };
				   if(docs.length == 1){
					   var b = docs[0];
					   socket.emit('CourseEditLog-callback', {magess: b.UserLog });
				   };
			  });
		  });
		  
		  // 发送通知
		  socket.on('emit-bell', function(data){
			  var a = data.magess;
			  var dbkey = db.collection('key');
			  dbkey.find({"_id" : a[2] }).toArray(function(err, docs){
				  if(err){
                      DeBug(err, socket);
                   };
				   if(docs.length == 1){
					   var b = new Object();
					   b = docs[0];
					   b.bell[b.bell.length] = a[1];
					   dbkey.updateOne({ "_id" : a[2]}, { $set: b }, function(err, result){
						   if(err){
							   DeBug(err, socket);
						   };
						   for(var c = 0, d = a[0]; c < d.length; c ++){
							   var e = d[c];
							   dbkey.find({"_id" : e }).toArray(function(err, docs){
								   if(err){
									   DeBug(err, socket);
						   			};
								   if(docs.length == 1){
									   var f = new Object();
									   f = docs[0];
									   f.bell[f.bell.length] = a[1];
									   dbkey.updateOne({ "_id" : e}, { $set: f }, function(err, result){
										   if(err){
											   DeBug(err, socket);
						   					};
										   if(c == d.length - 1){
											   if(result.result.n == 1){
												   socket.on('emit-bell-callback', {magess: [true, a[1]]});
											   };
										   };
									   });
								   };
							   });
						   };
					   });
				   };
			  });
		  });
		  
		  
		  // 商务合同
		  socket.on('Contract', function(data){
			  var a = data.magess;
			  var dbcompany = db.collection('company');
			  dbcompany.find({"_id" : a }).toArray(function(err, docs){
				  if(err){
                       DeBug(err, socket);
                   };
				  if(docs.length == 1){
					  var b = docs[0].Contract;
					  socket.emit('Contract-callback', {magess: b});
				  };
			  });
		  });
		  
		  // 新建合同
		  socket.on('adduser-addTotalcost', function(data){
			  var a = data.magess;
			  var dbcompany = db.collection('company');
			  dbcompany.find({"_id" : a[0] }).toArray(function(err, docs){
				  if(err){
                       DeBug(err, socket);
                   };
				  if(docs.length == 1){
					  var b = new Object();
					  var b = docs[0];
					  b.Contract[b.Contract.length] = a[1];
					  dbcompany.updateOne({ "_id" : a[0]}, { $set: b }, function(err, result){
						  if(err){
							  DeBug(err, socket);
						  };
						  if(result.result.n == 1){
							  socket.emit('adduser-addTotalcost-callback', {magess: true});
						  };
					  });
				  };
			  });
		  });
		  
		  // 永久删除课程
		  socket.on('PermanentDeleteCourse', function(data){
			  var a = data.magess;
			  var dbcourse = db.collection('course');
			  
			  dbcourse.find({"_id" : a[0] }).toArray(function(err, docs){
				   if(err){
                       DeBug(err, socket);
                    };
				    if(docs.length == 1){
						var b = new Object();
						b = docs[0];
						var c = b.allot;
						
						dbcourse.deleteOne({ "_id" : a[0] }, function(err, result) {
							if(err){
								DeBug(err, socket);
							};
							
							var dbkey = db.collection('key');
				  			dbkey.find({"_id" : a[1] }).toArray(function(err, docs){
								if(err){
									DeBug(err, socket);
								};
								if(docs.length == 1){
									var d = new Object();
									d = docs[0];
									
                                    for (var e = 0; e < d.course.length; e++){
                                        if (d.course[e] == a[0]) {
											d.course.splice(e, 1);
										};
										if(e == d.course.length - 1){
											gx();
										}else
										if(d.course.length == 0){
											gx();
										};
										
										var gx = function(){
											
											dbkey.updateOne({ "_id" : a[1]}, { $set: d }, function(err, result){
												if(err){
													DeBug(err, socket);
                                                };
												if(c != false){
													
													for(var g = 0; g < c.length; g ++){
														
														dbkey.find({"_id" : c[g] }).toArray(function(err, docs){
															if(err){
																DeBug(err, socket);
															};
															var h = new Object();
															h = docs[0];
															
															for(var i = 0, j = h.course; i < j.length; i ++){
																if(j[i] == a[0]){
																	h.splice(i, 1);
																};
																if(i == j.length - 1){
																	
																	dbkey.updateOne({ "_id" : h._id }, { $set: h }, function(err, result){
																		if(err){
																			DeBug(err, socket);
																		};
																		if(result.result.n == 0){
																			socket.emit('PermanentDeleteCourse-callback', {magess: false});
																		}else{
																			socket.emit('PermanentDeleteCourse-callback', {magess: true});
																		};
																	});
																};
															};
														});
													};
												};
											});
										};
									};
								};
                            });
                        });
					};
			  });
		  });
		  
		  
          
          // 恢复课程
		  socket.on('ClickCourseDateRestore', function(data){
			  var a = data.magess;
			  var dbcourse = db.collection('course');
			  dbcourse.find({"_id" : a }).toArray(function(err, docs){
				  if(err){
                      DeBug(err, socket);
                  };
				  var b = new Object();
				  b = docs[0];
				  b['y/n'] = true;
				  dbcourse.updateOne({ "_id" : a }, { $set: b }, function(err, result){
					  if(err){
                      	  DeBug(err, socket);
                  	   };
					  if(result.result.n == 0){
						  socket.emit('ClickCourseDateRestore-callback', {magess: false});
					  }else{
						  socket.emit('ClickCourseDateRestore-callback', {magess: true});
					  };
				  });
			  });
		  });
          
          // 新建共享课程
          socket.on('Share-course', function(data){
              var a = data.magess;
              var dbcourse = db.collection('course');
              dbcourse.find({"_id" : a[0] }).toArray(function(err, docs){
                  if(err){
                      DeBug(err, socket);
                  };
                  var b = new Object();
                  b = docs[0];
                  b.Shared = a[1];
                  dbcourse.updateOne({ "_id" : a[0]}, { $set: b }, function(err, result){
                      assert.equal(err, null);
                      if(err){
                          DeBug(err, socket);
                      };
                      if(result.result.n == 1){
                          socket.emit('Share-course-on', {magess: true});
                          var c = a[1];
                          for(var d = 0; d < c.length; d ++){
                               var dbkey = db.collection('key');
                               dbkey.find({"_id" : c[d] }).toArray(function(err, docs){
                                   var e = new Object();
                                   e = docs[0];
                                   if(e.course == false){
                                       var f = new Array();
                                       f[f.length] = a[0];
                                       e.course = f;
                                       return
                                   }else{
                                       var f = e.course;
                                       f[f.length] = a[0];
                                       e.course = f;
                                       return
                                   };
                                   
                                   dbkey.updateOne({ "_id" : c[d]}, { $set: e }, function(err, result){
                                       assert.equal(err, null);
                                       if(err){
                                           DeBug(err, socket);
                                       };
                                       if(result.result.n == 1){
                                           socket.emit('Share-course-on', {magess: true});
                                       }else
                                       if(result.result.n == 0){
                                           socket.emit('Share-course-on', {magess: false});
                                        };
                                   });
                               });
                          };
                      };
                  });
              });
          });
          
      });
  });